package com.jy.adscm.operation.mscyb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.cg.bean.CgMaterial;
import com.jy.adscm.operation.cg.mapper.mysql.CgMaterialMapper;
import com.jy.adscm.operation.mscyb.bean.*;
import com.jy.adscm.operation.mscyb.bean.JqfzIssueQs;
import com.jy.adscm.operation.mscyb.mapper.mysql.*;
import com.jy.adscm.operation.mscyb.service.JqfzIssueQsService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.operation.purchase.bean.Purchasebom;
import com.jy.adscm.operation.purchase.mapper.mysql.PurchasebomMapper;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.utils.AdscmDateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-06-14
 */
@Service
public class JqfzIssueQsServiceImpl extends ServiceImpl<JqfzIssueQsMapper, JqfzIssueQs> implements JqfzIssueQsService {

    @Autowired
    JyUserMapper jyUserMapper;
    @Autowired
    JqcsMachineTestMapper jqcsMachineTestMapper;
    @Autowired
    JqcsMachineTestProecssMapper jqcsMachineTestProecssMapper;
    @Autowired
    JqcsSettingMapper jqcsSettingMapper;
    @Autowired
    JqzzSettingMapper jqzzSettingMapper;
    @Autowired
    JqzzMachineMapper jqzzMachineMapper;
    @Autowired
    JqzzResultMapper jqzzResultMapper;
    @Autowired
    JqzzPartsMaterialMapper jqzzPartsMaterialMapper;
    @Autowired
    JqzzPartsMaterialListMapper jqzzPartsMaterialListMapper;
    @Autowired
    JqbzSettingsMapper jqbzSettingsMapper;
    @Autowired
    JqbzResultMapper jqbzResultMapper;
    @Autowired
    JqbzMaterialMapper jqbzMaterialMapper;
    @Autowired
    JqbzMaterialListMapper jqbzMaterialListMapper;
    @Autowired
    JqbzMaterialListCheckMapper jqbzMaterialListCheckMapper;

    @Autowired
    JqzzMaterialMapper jqzzMaterialMapper;
    @Autowired
    JqzzMaterialListMapper jqzzMaterialListMapper;
    @Autowired
    JqzzMaterialListCheckMapper jqzzMaterialListCheckMapper;
    @Autowired
    JqfzSettingMapper jqfzSettingMapper;
    @Autowired
    JqfzSettingProcessMapper jqfzSettingProcessMapper;
    @Autowired
    JqfzIssueQsProecssMapper jqfzIssueQsProecssMapper;
    @Autowired
    JqbzPartsassemblyMapper jqbzPartsassemblyMapper;
    @Autowired
    CgMaterialMapper cgMaterialMapper;
    @Override
    public Boolean updateJqfzIssueQs(JqfzIssueQs jqfzIssueQs, Token token) {
        //待办修改
        if (jqfzIssueQs.getTaskstate()==0 && jqfzIssueQs.getTaskfinishstate()==0 &&
                "0".equals(jqfzIssueQs.getIstaskstart()) &&  "0".equals(jqfzIssueQs.getIstaskbegin()) &&
                "0".equals(jqfzIssueQs.getIstaskend())  && "0".equals(jqfzIssueQs.getIsreviewtask())
        ){
            jqfzIssueQs.setTaskreleaseperson(token.getUser().getId().toString());
            jqfzIssueQs.setTaskreleaseaddtime(new Date());

            return  super.updateById(jqfzIssueQs);
        }
        //接收任务
        if (jqfzIssueQs.getTaskstate()==0 && jqfzIssueQs.getTaskfinishstate()==0 &&
                "1".equals(jqfzIssueQs.getIstaskstart()) &&  "0".equals(jqfzIssueQs.getIstaskbegin()) &&
                "0".equals(jqfzIssueQs.getIstaskend())  && "0".equals(jqfzIssueQs.getIsreviewtask())
        ){
            jqfzIssueQs.setIstaskstart("1");
            jqfzIssueQs.setTaskstartaddtime(new Date());
            jqfzIssueQs.setTaskstate(1);
            return  super.updateById(jqfzIssueQs);
        }
        //开始任务
        if (jqfzIssueQs.getTaskstate()==1 && jqfzIssueQs.getTaskfinishstate()==0 &&
                "1".equals(jqfzIssueQs.getIstaskstart()) &&  "1".equals(jqfzIssueQs.getIstaskbegin()) &&
                "0".equals(jqfzIssueQs.getIstaskend())  && "0".equals(jqfzIssueQs.getIsreviewtask())
        ){
            jqfzIssueQs.setIstaskbegin("1");
            jqfzIssueQs.setTaskbeginaddtime(new Date());
            return super.updateById(jqfzIssueQs);
        }
        //结束任务
        if (jqfzIssueQs.getTaskstate()==1 && jqfzIssueQs.getTaskfinishstate()==0 &&
                "1".equals(jqfzIssueQs.getIstaskstart()) &&  "1".equals(jqfzIssueQs.getIstaskbegin()) &&
                "1".equals(jqfzIssueQs.getIstaskend())  && "0".equals(jqfzIssueQs.getIsreviewtask())
        ){
            jqfzIssueQs.setIstaskend("1");
            jqfzIssueQs.setTaskfinishstate(1);
            jqfzIssueQs.setTaskendaddtime(new Date());
            //生成打包记录
            return super.updateById(jqfzIssueQs);
        }
        //复核任务
        if (jqfzIssueQs.getTaskstate()==1 && jqfzIssueQs.getTaskfinishstate()==1 &&
                "1".equals(jqfzIssueQs.getIstaskstart()) &&  "1".equals(jqfzIssueQs.getIstaskbegin()) &&
                "1".equals(jqfzIssueQs.getIstaskend())  && "1".equals(jqfzIssueQs.getIsreviewtask())
        ){
            jqfzIssueQs.setIsreviewtask("1");
            jqfzIssueQs.setReviewaddtime(new Date());
            jqfzIssueQs.setReviewperson(token.getUser().getId().toString());
            return super.updateById(jqfzIssueQs);
        }

        return null;
    }

    @Override
    public Page<JqfzIssueQs> selectJqfzIssueQs(Page<JqfzIssueQs> page ,QueryJqfzIssueQs queryJqfzIssueQs) {
        Wrapper<JqfzIssueQs> wrapper =new EntityWrapper<>();
        wrapper.eq("TASKSTATE",queryJqfzIssueQs.getTaskState());
        wrapper.eq("TASKFINISHSTATE",queryJqfzIssueQs.getTaskFinishState());
        wrapper.eq("isReviewTask",queryJqfzIssueQs.getIsReviewTask());
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryJqfzIssueQs.getCode(),null)){
            wrapper.like("TASKCODE",queryJqfzIssueQs.getCode());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryJqfzIssueQs.getName(),null)){
            wrapper.eq("RESOURCENAME",queryJqfzIssueQs.getName());
        }
        page = super.selectPage(page,wrapper);
        List<JqfzIssueQs> JqfzIssueQss = page.getRecords();
        for (JqfzIssueQs  jqfzIssueQs : JqfzIssueQss) {
            if ( jqfzIssueQs.getTaskreleaseperson()!=null && !"".equals( jqfzIssueQs.getTaskreleaseperson())){
                JyUser jyUser = jyUserMapper.selectById(jqfzIssueQs.getTaskreleaseperson());
                jqfzIssueQs.setTaskreleasepersonName(jyUser.getName());
            }
            if ( jqfzIssueQs.getTaskperformperson()!=null && !"".equals( jqfzIssueQs.getTaskperformperson())){
                JyUser jyUser = jyUserMapper.selectById(jqfzIssueQs.getTaskperformperson());
                jqfzIssueQs.setTaskperformpersonName(jyUser.getName());
            }

            if ( jqfzIssueQs.getReviewperson()!=null && !"".equals( jqfzIssueQs.getReviewperson())){
                JyUser jyUser = jyUserMapper.selectById(jqfzIssueQs.getReviewperson());
                jqfzIssueQs.setReviewpersonName(jyUser.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(jqfzIssueQs.getResourcename(),null)){
            }
        }
        page.setTotal(selectCount(wrapper));
        return page;
    }

    @Override
    public List<JyUser> selectTaskPerformPerson() {
        List<JyUser> jyUsers = jyUserMapper.selectTaskPerformPerson("97");
        return jyUsers;
    }



    @Override
    public List<JqCheckRecoder>  selectJqcheckRecoder(String code) {
        //新建jqCheckRecoder 对象
        List<JqCheckRecoder>   jqCheckRecoders =new ArrayList<>();
        //先根据输入的code 机器编号查询 出厂检验的记录
        Wrapper<JqcsMachineTest> jqcsMachineTestWrapper =new EntityWrapper<>();
        jqcsMachineTestWrapper.eq("TASKCODE",code);
        List<JqcsMachineTest> jqcsMachineTests = jqcsMachineTestMapper.selectList(jqcsMachineTestWrapper);
        for (JqcsMachineTest jqcsMachineTest : jqcsMachineTests) {
            JqcsSetting jqcsSetting = jqcsSettingMapper.selectById(jqcsMachineTest.getResourcename());
            JqCheckRecoder jqCheckRecoder =new JqCheckRecoder();
            jqCheckRecoder.setId(jqcsMachineTest.getTid());
            jqCheckRecoder.setName(jqcsSetting.getName());
            jqCheckRecoder.setCode(jqcsSetting.getCode());
            jqCheckRecoder.setState(jqcsMachineTest.getState());
           //查询这个出厂检验的所有数据
            Wrapper<JqcsMachineTestProecss> jqcsMachineTestProecssWrapper =new EntityWrapper<>();
            jqcsMachineTestProecssWrapper.eq("pid",jqcsMachineTest.getTid());
            jqcsMachineTestProecssWrapper.orderBy("rank",false);
            List<JqcsMachineTestProecss> jqcsMachineTestProecsses = jqcsMachineTestProecssMapper.selectList(jqcsMachineTestProecssWrapper);
            if (jqcsMachineTestProecsses!=null &&jqcsMachineTestProecsses.size()>0){
                JqcsMachineTestProecss jqcsMachineTestProecss = jqcsMachineTestProecsses.get(0);
                JyUser jyUser = jyUserMapper.selectById(jqcsMachineTestProecss.getCreator());
                if (AdscmBaseUtils.getInstance().isNotEmpty(jyUser,null)){
                    jqCheckRecoder.setCreator(jyUser.getName());
                }
                jqCheckRecoder.setCreateTime(jqcsMachineTestProecss.getCreatTime());
            }
            //遍历检验记录
            List<JqCheck> jqChecks=new ArrayList<>();
            for (JqcsMachineTestProecss jqcsMachineTestProecss : jqcsMachineTestProecsses) {
                JqCheck jqCheck =new JqCheck();
                jqCheck.setName(jqcsMachineTestProecss.getName());
                jqCheck.setValue(jqcsMachineTestProecss.getValue());
                jqCheck.setType(jqcsMachineTestProecss.getType());
                jqCheck.setReference(jqcsMachineTestProecss.getReference());
                jqChecks.add(jqCheck);
            }
            jqCheckRecoder.setJqChecks(jqChecks);
            jqCheckRecoders.add(jqCheckRecoder);
        }
        //总装的检测
         Wrapper<JqzzMachine> jqzzMachineWrapper=new EntityWrapper<>();
        jqzzMachineWrapper.eq("TASKCODE",code);
        List<JqzzMachine> jqzzMachines = jqzzMachineMapper.selectList(jqzzMachineWrapper);
        for (JqzzMachine jqzzMachine : jqzzMachines) {
            JqCheckRecoder jqzzMachineJqCheckRecoder= new JqCheckRecoder();
            JqzzSetting jqzzSetting = jqzzSettingMapper.selectById(jqzzMachine.getResourcename());
            jqzzMachineJqCheckRecoder.setId(jqzzMachine.getTid());
            jqzzMachineJqCheckRecoder.setName(jqzzSetting.getName());
            jqzzMachineJqCheckRecoder.setCode(jqzzSetting.getCode());
            jqzzMachineJqCheckRecoder.setState(jqzzMachine.getState());
            //查询这个总装完成检验记录
            Wrapper<JqzzResult> jqzzResultWrapper =new EntityWrapper<>();
            jqzzResultWrapper.eq("pid",jqzzMachine.getTid());
            jqzzResultWrapper.orderBy("rank",false);
            List<JqzzResult> jqzzResults = jqzzResultMapper.selectList(jqzzResultWrapper);
            if (jqzzResults!=null && jqzzResults.size()>0){
                JqzzResult jqzzResult = jqzzResults.get(0);
                JyUser jyUser = jyUserMapper.selectById(jqzzResult.getCreator());
                if (AdscmBaseUtils.getInstance().isNotEmpty(jyUser,null)){
                    jqzzMachineJqCheckRecoder.setCreator(jyUser.getName());
                }
                jqzzMachineJqCheckRecoder.setCreateTime(jqzzResult.getCreateTime());
            }
            //遍历检测结果
            List<JqCheck>  jqzzResultjJqChecks =new ArrayList<>();
            for (JqzzResult jqzzResult : jqzzResults) {
                JqCheck jqCheck =new JqCheck();
                jqCheck.setName(jqzzResult.getName());
                jqCheck.setValue(jqzzResult.getValue());
                jqCheck.setType(jqzzResult.getType());
                jqCheck.setReference(jqzzResult.getReference());
                jqzzResultjJqChecks.add(jqCheck);
            }
            jqzzMachineJqCheckRecoder.setJqChecks(jqzzResultjJqChecks);
            jqCheckRecoders.add(jqzzMachineJqCheckRecoder);
            //查询这个总装使用的部件
            Wrapper<JqzzPartsMaterial> jqzzPartsMaterialWrapper =new EntityWrapper<>();
            jqzzPartsMaterialWrapper.eq("tid",jqzzMachine.getTid());
            List<JqzzPartsMaterial> jqzzPartsMaterials = jqzzPartsMaterialMapper.selectList(jqzzPartsMaterialWrapper);
            //部件列表
            for (JqzzPartsMaterial jqzzPartsMaterial : jqzzPartsMaterials) {
                JqCheckRecoder partsJqCheckRecoder=new JqCheckRecoder();
                JqbzSettings jqbzSettings = jqbzSettingsMapper.selectById(jqzzPartsMaterial.getName());
                partsJqCheckRecoder.setId(jqzzPartsMaterial.getId());
                partsJqCheckRecoder.setName(jqbzSettings.getName());
                partsJqCheckRecoder.setCode(jqbzSettings.getCode());
                partsJqCheckRecoder.setState(jqzzPartsMaterial.getIsAssembly());
                //查询这个部装的检测记录
                List<JqCheck> partsJqcheck=new ArrayList<>();
                Wrapper<JqzzPartsMaterialList> jqzzPartsMaterialListWrapper=new EntityWrapper<>();
                jqzzPartsMaterialListWrapper.eq("pid", jqzzPartsMaterial.getId());
                jqzzPartsMaterialListWrapper.eq("state",1);
                List<JqzzPartsMaterialList> jqzzPartsMaterialLists = jqzzPartsMaterialListMapper.selectList(jqzzPartsMaterialListWrapper);
                //使用部件列表遍历
                for (JqzzPartsMaterialList jqzzPartsMaterialList : jqzzPartsMaterialLists) {
                    Wrapper<JqbzResult> jqbzResultWrapper=new EntityWrapper<>();
                    jqbzResultWrapper.eq("pid",jqzzPartsMaterialList.getName());
                    List<JqbzResult> jqbzResults = jqbzResultMapper.selectList(jqbzResultWrapper);
                    if (jqbzResults!=null&& jqbzResults.size()>0){
                        JqbzResult jqbzResult = jqbzResults.get(0);
                        JyUser jyUser = jyUserMapper.selectById(jqbzResult.getCreator());
                        if (AdscmBaseUtils.getInstance().isNotEmpty(jyUser,null)){
                            partsJqCheckRecoder.setCreator(jyUser.getName());
                        }
                        partsJqCheckRecoder.setCreateTime(jqbzResult.getCreateTime());
                    }
                    //遍历检测结果
                    for (JqbzResult jqbzResult : jqbzResults) {
                        JqCheck jqCheck =new JqCheck();
                        jqCheck.setName(jqbzResult.getName());
                        jqCheck.setValue(jqbzResult.getValue());
                        jqCheck.setReference(jqbzResult.getReference());
                        jqCheck.setType(jqbzResult.getType());
                        partsJqcheck.add(jqCheck);
                    }
                    JqCheck jqCheck1 =new JqCheck();
                    jqCheck1.setName("检测人");
                    if (AdscmBaseUtils.getInstance().isNotEmpty(partsJqCheckRecoder.getCreator(),null)){
                        jqCheck1.setValue(partsJqCheckRecoder.getCreator());
                    }
                    jqCheck1.setType("0");
                    JqCheck jqCheck2 =new JqCheck();
                    jqCheck2.setName("检测时间");
                    if (AdscmBaseUtils.getInstance().isNotEmpty(partsJqCheckRecoder.getCreateTime(),null)){
                        jqCheck2.setValue(AdscmDateUtils.getDateFomat(partsJqCheckRecoder.getCreateTime()));
                    }
                    jqCheck2.setType("0");
                    partsJqcheck.add(jqCheck1);
                    partsJqcheck.add(jqCheck2);
                    //根据部件的id查询部件使用的零件列表
                    Wrapper<JqbzMaterial> jqbzMaterialWrapper =new EntityWrapper<>();
                    jqbzMaterialWrapper.eq("tid",jqzzPartsMaterialList.getName());
                    List<JqbzMaterial> jqbzMaterials = jqbzMaterialMapper.selectList(jqbzMaterialWrapper);
                    //遍历部件物料列表
                    for (JqbzMaterial jqbzMaterial : jqbzMaterials) {
                        JqCheckRecoder jqbzMaterialJqCheckRecoder=new JqCheckRecoder();
                        CgMaterial cgMaterial = cgMaterialMapper.selectById(jqbzMaterial.getName());
                        jqbzMaterialJqCheckRecoder.setId(jqbzMaterial.getId());
                        jqbzMaterialJqCheckRecoder.setName(cgMaterial.getName());
                        jqbzMaterialJqCheckRecoder.setCode(cgMaterial.getCode());
                        jqbzMaterialJqCheckRecoder.setState(jqbzMaterial.getIsAssembly());
                        //查询个物料的详细用料
                        List<JqCheck> jqbzMaterialJqChecks=new ArrayList<>();
                        Wrapper<JqbzMaterialList> jqbzMaterialListWrapper =new EntityWrapper<>();
                        jqbzMaterialListWrapper.eq("pid",jqbzMaterial.getId());
                        jqbzMaterialListWrapper.eq("state",1);
                        List<JqbzMaterialList> jqbzMaterialLists = jqbzMaterialListMapper.selectList(jqbzMaterialListWrapper);
                        //查询这个的检测记录
                        for (JqbzMaterialList jqbzMaterialList : jqbzMaterialLists) {
                            Wrapper<JqbzMaterialListCheck> jqbzMaterialListCheckWrapper=new EntityWrapper<>();
                            jqbzMaterialListCheckWrapper.eq("pid",jqbzMaterialList.getId());
                            List<JqbzMaterialListCheck> jqbzMaterialListChecks = jqbzMaterialListCheckMapper.selectList(jqbzMaterialListCheckWrapper);
                            if (jqbzMaterialListChecks!=null&&jqbzMaterialListChecks.size()>0){
                                JqbzMaterialListCheck jqbzMaterialListCheck = jqbzMaterialListChecks.get(0);
                                jqbzMaterialJqCheckRecoder.setCreateTime(jqbzMaterialListCheck.getCreateTime());
                                JyUser jyUser = jyUserMapper.selectById(jqbzMaterialListCheck.getCreator());
                                if (AdscmBaseUtils.getInstance().isNotEmpty(jyUser,null)){
                                    jqbzMaterialJqCheckRecoder.setCreator(jyUser.getName());
                                }
                                for (JqbzMaterialListCheck materialListCheck : jqbzMaterialListChecks) {
                                    JqCheck  jqCheck =new JqCheck();
                                    jqCheck.setName(materialListCheck.getName());
                                    jqCheck.setValue(materialListCheck.getValue());
                                    jqCheck.setType(materialListCheck.getType());
                                    jqCheck.setReference(materialListCheck.getReference());
                                    jqbzMaterialJqChecks.add(jqCheck);
                                }
                                JqCheck jqCheck3 =new JqCheck();
                                jqCheck3.setName("检测人");
                                if (AdscmBaseUtils.getInstance().isNotEmpty(jqbzMaterialJqCheckRecoder.getCreator(),null)){
                                    jqCheck3.setValue(jqbzMaterialJqCheckRecoder.getCreator());
                                }

                                jqCheck3.setType("0");
                                JqCheck jqCheck4 =new JqCheck();
                                jqCheck4.setName("检测时间");
                                if (AdscmBaseUtils.getInstance().isNotEmpty(jqbzMaterialJqCheckRecoder.getCreateTime(),null)){
                                    jqCheck4.setValue(AdscmDateUtils.getDateFomat(jqbzMaterialJqCheckRecoder.getCreateTime()));
                                }
                                jqCheck4.setType("0");
                                jqbzMaterialJqChecks.add(jqCheck3);
                                jqbzMaterialJqChecks.add(jqCheck4);

                            }
                        }
                        if (jqbzMaterialJqChecks.size()>0){
                            jqbzMaterialJqCheckRecoder.setJqChecks(jqbzMaterialJqChecks);
                            jqCheckRecoders.add(jqbzMaterialJqCheckRecoder);
                        }

                    }

                }
                partsJqCheckRecoder.setJqChecks(partsJqcheck);
                jqCheckRecoders.add(partsJqCheckRecoder);
            }
            //查询部件下面使用的零件列表
           Wrapper<JqzzMaterial> jqzzMaterialWrapper=new EntityWrapper<>();
            jqzzMaterialWrapper.eq("tid",jqzzMachine.getTid());
            List<JqzzMaterial> jqzzMaterials = jqzzMaterialMapper.selectList(jqzzMaterialWrapper);
            //遍历物料列表
            for (JqzzMaterial jqzzMaterial : jqzzMaterials) {
                JqCheckRecoder   jqzzMaterialJqCheckRecoder =new JqCheckRecoder();
                CgMaterial cgMaterial = cgMaterialMapper.selectById(jqzzMaterial.getName());
                jqzzMaterialJqCheckRecoder.setId(jqzzMaterial.getId());
                jqzzMaterialJqCheckRecoder.setName(cgMaterial.getName());
                jqzzMaterialJqCheckRecoder.setCode(cgMaterial.getCode());
                jqzzMaterialJqCheckRecoder.setState(jqzzMaterial.getIsAssembly());
                //查询详细的用料情况
                List<JqCheck> jqzzMaterialJqCheck =new ArrayList<>();
                Wrapper<JqzzMaterialList> jqzzMaterialListWrapper =new EntityWrapper<>();
                jqzzMaterialListWrapper.eq("pid",jqzzMaterial.getId());
                jqzzMaterialListWrapper.eq("state",1);
                List<JqzzMaterialList> jqzzMaterialLists = jqzzMaterialListMapper.selectList(jqzzMaterialListWrapper);
                for (JqzzMaterialList jqzzMaterialList : jqzzMaterialLists) {
                     Wrapper<JqzzMaterialListCheck>  jqzzMaterialListCheckWrapper =new EntityWrapper<>();
                    jqzzMaterialListCheckWrapper.eq("pid",jqzzMaterialList.getId());
                    List<JqzzMaterialListCheck> jqzzMaterialListChecks = jqzzMaterialListCheckMapper.selectList(jqzzMaterialListCheckWrapper);
                    if (jqzzMaterialListChecks!=null &&jqzzMaterialListChecks.size()>0){
                        JqzzMaterialListCheck jqzzMaterialListCheck = jqzzMaterialListChecks.get(0);
                        jqzzMaterialJqCheckRecoder.setCreateTime(jqzzMaterialListCheck.getCreateTime());
                        if (AdscmBaseUtils.getInstance().isNotEmpty(jqzzMaterialListCheck.getCreator(),null)){
                            JyUser jyUser = jyUserMapper.selectById(jqzzMaterialListCheck.getCreator());
                            if (AdscmBaseUtils.getInstance().isNotEmpty(jyUser,null)){
                                jqzzMaterialJqCheckRecoder.setCreator(jyUser.getName());
                            }
                        }
                        for (JqzzMaterialListCheck materialListCheck : jqzzMaterialListChecks) {
                            JqCheck  jqCheck =new JqCheck();
                            jqCheck.setName(materialListCheck.getName());
                            jqCheck.setValue(materialListCheck.getValue());
                            jqCheck.setType(materialListCheck.getType());
                            jqCheck.setReference(materialListCheck.getReference());
                            jqzzMaterialJqCheck.add(jqCheck);
                        }
                        JqCheck jqCheck3 =new JqCheck();
                        jqCheck3.setName("检测人");
                        if (AdscmBaseUtils.getInstance().isNotEmpty(jqzzMaterialJqCheckRecoder.getCreator(),null)){
                            jqCheck3.setValue(jqzzMaterialJqCheckRecoder.getCreator());
                        }
                        jqCheck3.setType("0");
                        JqCheck jqCheck4 =new JqCheck();
                        jqCheck4.setName("检测时间");
                        jqCheck4.setType("0");
                        if (AdscmBaseUtils.getInstance().isNotEmpty(jqzzMaterialJqCheckRecoder.getCreateTime(),null)){
                            jqCheck4.setValue(AdscmDateUtils.getDateFomat(jqzzMaterialJqCheckRecoder.getCreateTime()));
                        }
                        jqzzMaterialJqCheck.add(jqCheck3);
                        jqzzMaterialJqCheck.add(jqCheck4);
                    }
                }
                if (jqzzMaterialJqCheck.size()>0){
                    jqzzMaterialJqCheckRecoder.setJqChecks(jqzzMaterialJqCheck);
                    jqCheckRecoders.add(jqzzMaterialJqCheckRecoder);
                }

            }

        }

        return jqCheckRecoders;
    }

    @Override
    public boolean insertJqfzIssueQs(JqfzIssueQs jqfzIssueQs) throws AdscmSystemException {
        //根据机器编号查询是否还有存在这个编号
        String taskCode = jqfzIssueQs.getResourcename();
        Wrapper<JqzzMachine> jqzzMachineWrapper =new EntityWrapper<>();
        jqzzMachineWrapper.eq("TASKCODE",jqfzIssueQs.getResourcename());
        List<JqzzMachine> jqzzMachines = jqzzMachineMapper.selectList(jqzzMachineWrapper);
        if (!(jqzzMachines!=null && jqzzMachines.size()>0)){
            throw  new AdscmSystemException("不存在这样的机器编号");
        }
        Wrapper<JqfzIssueQs> jqfzIssueQsWrapper =new EntityWrapper<>();
        jqfzIssueQsWrapper.eq("TASKCODE",jqfzIssueQs.getResourcename());
        List<JqfzIssueQs> jqfzIssueQses = super.selectList(jqfzIssueQsWrapper);
        if (jqfzIssueQses!=null &&jqfzIssueQses.size()>0 ){
            throw  new AdscmSystemException("已经存在了这个机器编号发证信息");
        }
        //查询机器
        JqzzMachine jqzzMachine = jqzzMachines.get(0);
        JqzzSetting jqzzSetting = jqzzSettingMapper.selectById(jqzzMachine.getResourcename());
        Wrapper<JqfzSetting> jqfzSettingWrapper =new EntityWrapper<>();
        jqfzSettingWrapper.eq("type",jqzzSetting.getType());
        jqfzSettingWrapper.eq("isEnable",1);
        List<JqfzSetting> jqfzSettings = jqfzSettingMapper.selectList(jqfzSettingWrapper);
        for (JqfzSetting jqfzSetting : jqfzSettings) {
            jqfzIssueQs.setBatchnums(jqzzMachine.getBatchnums());
            jqfzIssueQs.setTypenums(jqzzMachine.getTypenums());
            jqfzIssueQs.setTaskcode(taskCode);
            jqfzIssueQs.setResourcename(jqfzSetting.getId().toString());
            jqfzIssueQs.setTaskreleaseaddtime(new Date());
            jqfzIssueQs.setTaskfinishstate(0);
            jqfzIssueQs.setTaskstate(0);
            jqfzIssueQs.setIstaskstart("0");
            jqfzIssueQs.setIstaskbegin("0");
            jqfzIssueQs.setIstaskend("0");
            jqfzIssueQs.setIsreviewtask("0");
            jqfzIssueQs.setReviewresult("-1");
            super.insert(jqfzIssueQs);
            //
            Wrapper<JqfzSettingProcess> jqfzSettingProcessWrapper=new EntityWrapper<>();
            jqfzSettingProcessWrapper.eq("pid" ,jqfzSetting.getId());
            List<JqfzSettingProcess> jqfzSettingProcesses = jqfzSettingProcessMapper.selectList(jqfzSettingProcessWrapper);
            for (JqfzSettingProcess jqfzSettingProces : jqfzSettingProcesses) {
                JqfzIssueQsProecss jqfzIssueQsProecss =new JqfzIssueQsProecss();
                jqfzIssueQsProecss.setPid(jqfzIssueQs.getTid());
                jqfzIssueQsProecss.setName(jqfzSettingProces.getName());
                jqfzIssueQsProecss.setCid(jqfzSettingProces.getId());
                jqfzIssueQsProecss.setReference(jqfzSettingProces.getReference());
                jqfzIssueQsProecss.setRemark(jqfzSettingProces.getRemark());
                jqfzIssueQsProecss.setRank(jqfzSettingProces.getRank());
                jqfzIssueQsProecss.setType(jqfzSettingProces.getType());
                jqfzIssueQsProecss.setUnit(jqfzSettingProces.getUnit());
                jqfzIssueQsProecssMapper.insert(jqfzIssueQsProecss);
            }

        }

        return false;
    }

    @Override
    public List<JqcsMachineTest> selectJqcs(String code) {
        Wrapper<JqcsMachineTest> wrapper =new EntityWrapper<>();
        wrapper.eq("TASKCODE",code);
        List<JqcsMachineTest> jqcsMachineTests = jqcsMachineTestMapper.selectList(wrapper);
        for (JqcsMachineTest jqcsMachineTest : jqcsMachineTests) {
            if ( jqcsMachineTest.getTaskreleaseperson()!=null && !"".equals( jqcsMachineTest.getTaskreleaseperson())){
                JyUser jyUser = jyUserMapper.selectById(jqcsMachineTest.getTaskreleaseperson());
                jqcsMachineTest.setTaskreleasepersonName(jyUser.getName());
            }
            if ( jqcsMachineTest.getTaskperformperson()!=null && !"".equals( jqcsMachineTest.getTaskperformperson())){
                JyUser jyUser = jyUserMapper.selectById(jqcsMachineTest.getTaskperformperson());
                jqcsMachineTest.setTaskperformpersonName(jyUser.getName());
            }

            if ( jqcsMachineTest.getReviewperson()!=null && !"".equals( jqcsMachineTest.getReviewperson())){
                JyUser jyUser = jyUserMapper.selectById(jqcsMachineTest.getReviewperson());
                jqcsMachineTest.setReviewpersonName(jyUser.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(jqcsMachineTest.getResourcename(),null)){
                JqcsSetting jqcsSetting = jqcsSettingMapper.selectById(jqcsMachineTest.getResourcename());
                jqcsMachineTest.setResourcenamename(jqcsSetting.getName());
            }
        }
        return jqcsMachineTests;
    }

    @Override
    public List<JqzzMachine> selectJqzz(String code) {
        Wrapper<JqzzMachine> wrapper=new EntityWrapper<>();
        wrapper.eq("TASKCODE",code);
        List<JqzzMachine> jqzzMachines = jqzzMachineMapper.selectList(wrapper);
        for (JqzzMachine jqzzMachine : jqzzMachines) {
            if (jqzzMachine.getTaskreleaseperson() != null && !"".equals(jqzzMachine.getTaskreleaseperson())) {
                JyUser jyUser = jyUserMapper.selectById(jqzzMachine.getTaskreleaseperson());
                jqzzMachine.setTaskreleasepersonName(jyUser.getName());
            }
            if (jqzzMachine.getTaskperformperson() != null && !"".equals(jqzzMachine.getTaskperformperson())) {
                JyUser jyUser = jyUserMapper.selectById(jqzzMachine.getTaskperformperson());
                jqzzMachine.setTaskperformpersonName(jyUser.getName());
            }

            if (jqzzMachine.getReviewperson() != null && !"".equals(jqzzMachine.getReviewperson())) {
                JyUser jyUser = jyUserMapper.selectById(jqzzMachine.getReviewperson());
                jqzzMachine.setReviewpersonName(jyUser.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(jqzzMachine.getResourcename(), null)) {

                JqzzSetting jqzzSetting = jqzzSettingMapper.selectById(jqzzMachine.getResourcename());
                jqzzMachine.setResourcenameName(jqzzSetting.getName());
            }
        }
        return jqzzMachines;
    }

    @Override
    public List<JqbzPartsassembly> selectJqbz(String code) {
        List<JqbzPartsassembly> reuslt =new ArrayList<>();
        Wrapper<JqzzMachine> wrapper=new EntityWrapper<>();
        wrapper.eq("TASKCODE",code);
        List<JqzzMachine> jqzzMachines = jqzzMachineMapper.selectList(wrapper);
        for (JqzzMachine jqzzMachine : jqzzMachines) {
            //根据这个id查询部件列表
            Wrapper<JqzzPartsMaterial> jqzzPartsMaterialWrapper=new EntityWrapper<>();
            jqzzPartsMaterialWrapper.eq("tid",jqzzMachine.getTid());
            List<JqzzPartsMaterial> jqzzPartsMaterials = jqzzPartsMaterialMapper.selectList(jqzzPartsMaterialWrapper);
            for (JqzzPartsMaterial jqzzPartsMaterial : jqzzPartsMaterials) {
                Wrapper<JqzzPartsMaterialList> jqzzPartsMaterialListWrapper=new EntityWrapper<>();
                jqzzPartsMaterialListWrapper.eq("pid",jqzzPartsMaterial.getId());
                List<JqzzPartsMaterialList> jqzzPartsMaterialLists = jqzzPartsMaterialListMapper.selectList(jqzzPartsMaterialListWrapper);
                for (JqzzPartsMaterialList jqzzPartsMaterialList : jqzzPartsMaterialLists) {
                    JqbzPartsassembly jqbzPartsassembly = jqbzPartsassemblyMapper.selectById(Integer.valueOf(jqzzPartsMaterialList.getName()));
                    if ( jqbzPartsassembly.getTaskreleaseperson()!=null && !"".equals( jqbzPartsassembly.getTaskreleaseperson())){
                        JyUser jyUser = jyUserMapper.selectById(jqbzPartsassembly.getTaskreleaseperson());
                        jqbzPartsassembly.setTaskreleasepersonName(jyUser.getName());
                    }
                    if ( jqbzPartsassembly.getTaskperformperson()!=null && !"".equals( jqbzPartsassembly.getTaskperformperson())){
                        JyUser jyUser = jyUserMapper.selectById(jqbzPartsassembly.getTaskperformperson());
                        jqbzPartsassembly.setTaskperformpersonName(jyUser.getName());
                    }

                    if ( jqbzPartsassembly.getReviewperson()!=null && !"".equals( jqbzPartsassembly.getReviewperson())){
                        JyUser jyUser = jyUserMapper.selectById(jqbzPartsassembly.getReviewperson());
                        jqbzPartsassembly.setReviewpersonName(jyUser.getName());
                    }
                    if ( jqbzPartsassembly.getTasknums()!=null && !"".equals( jqbzPartsassembly.getTasknums())){
                        JyUser jyUser = jyUserMapper.selectById(jqbzPartsassembly.getTasknums());
                        jqbzPartsassembly.setTasknumsName(jyUser.getName());
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(jqbzPartsassembly.getResourcename(),null)){
                        JqbzSettings jqbzSettings = jqbzSettingsMapper.selectById( Integer.valueOf(jqbzPartsassembly.getResourcename()));
                        if (AdscmBaseUtils.getInstance().isNotEmpty(jqbzSettings,null)){
                            jqbzPartsassembly.setResourcenameName(jqbzSettings.getName());
                        }else {
                            jqbzPartsassembly.setResourcenameName(jqbzPartsassembly.getResourcename());
                        }
                    }
                    reuslt.add(jqbzPartsassembly);
                }
            }
        }
        return reuslt;
    }

    @Override
    public List<JqCheckRecoder> selectQcJqcheckRecoder(String code) {
        //先根据机器编号把


        return null;
    }
}
